package com.streetlight.lampcursor.ui.control

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.lampcursor.streetlight.R
import com.lampcursor.streetlight.databinding.FragmentControlBinding
import com.streetlight.lampcursor.model.LightStatus
import com.streetlight.lampcursor.model.LightMode

class ControlFragment : Fragment() {

    private var _binding: FragmentControlBinding? = null
    private val binding get() = _binding!!
    
    private val viewModel: ControlViewModel by viewModels()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentControlBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        
        setupViews()
        setupObservers()
        setupClickListeners()
    }

    private fun setupViews() {
        // 初始化视图状态
        updateLightStatus(LightStatus.OFF)
        updateLightMode(LightMode.MANUAL)
    }

    private fun setupObservers() {
        viewModel.lightStatus.observe(viewLifecycleOwner) { status ->
            updateLightStatus(status)
        }
        
        viewModel.lightMode.observe(viewLifecycleOwner) { mode ->
            updateLightMode(mode)
        }
        
        viewModel.brightness.observe(viewLifecycleOwner) { brightness ->
            updateBrightness(brightness)
        }
        
        viewModel.operationResult.observe(viewLifecycleOwner) { result ->
            when (result) {
                is OperationResult.Success -> {
                    Toast.makeText(context, result.message, Toast.LENGTH_SHORT).show()
                }
                is OperationResult.Error -> {
                    Toast.makeText(context, result.message, Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    private fun setupClickListeners() {
        // 开关按钮
        binding.btnTurnOn.setOnClickListener {
            viewModel.turnOnLight()
        }
        
        binding.btnTurnOff.setOnClickListener {
            viewModel.turnOffLight()
        }
        
        // 模式按钮
        binding.btnAutoMode.setOnClickListener {
            viewModel.setAutoMode()
        }
        
        binding.btnManualMode.setOnClickListener {
            viewModel.setManualMode()
        }
        
        // 亮度调节
        binding.seekbarBrightness.setOnSeekBarChangeListener(object : android.widget.SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: android.widget.SeekBar?, progress: Int, fromUser: Boolean) {
                if (fromUser) {
                    viewModel.setBrightness(progress)
                }
            }
            
            override fun onStartTrackingTouch(seekBar: android.widget.SeekBar?) {}
            override fun onStopTrackingTouch(seekBar: android.widget.SeekBar?) {}
        })
        
        // 快速设置开关
        binding.switchAutoBrightness.setOnCheckedChangeListener { _, isChecked ->
            viewModel.setAutoBrightness(isChecked)
        }
        
        binding.switchMotionSensor.setOnCheckedChangeListener { _, isChecked ->
            viewModel.setMotionSensor(isChecked)
        }
        
        binding.switchWeatherAdaptation.setOnCheckedChangeListener { _, isChecked ->
            viewModel.setWeatherAdaptation(isChecked)
        }
    }

    private fun updateLightStatus(status: LightStatus) {
        when (status) {
            LightStatus.ON -> {
                binding.ivLightStatus.setImageResource(R.drawable.ic_light_on)
                binding.tvLightStatus.text = "开启"
            }
            LightStatus.OFF -> {
                binding.ivLightStatus.setImageResource(R.drawable.ic_light_off)
                binding.tvLightStatus.text = "关闭"
            }
            LightStatus.AUTO -> {
                binding.ivLightStatus.setImageResource(R.drawable.ic_light_auto)
                binding.tvLightStatus.text = "自动"
            }
            LightStatus.ERROR -> {
                binding.ivLightStatus.setImageResource(R.drawable.ic_light_error)
                binding.tvLightStatus.text = "故障"
            }
        }
    }

    private fun updateLightMode(mode: LightMode) {
        when (mode) {
            LightMode.AUTO -> {
                binding.tvLightMode.text = "自动模式"
            }
            LightMode.MANUAL -> {
                binding.tvLightMode.text = "手动模式"
            }
        }
    }

    private fun updateBrightness(brightness: Int) {
        binding.tvBrightnessValue.text = getString(R.string.text_current_brightness, brightness)
        binding.seekbarBrightness.progress = brightness
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
} 